package com.example.demo.dao;

import com.example.demo.model.News;
import org.apache.ibatis.jdbc.SQL;

public class NewsSqlProvider {

    /**
     * 根据字段是否为空保存 
     * @param record News
     * @return String
     */
    public String insertSelective(News record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("news");
        
        if (record.getId() != null) {
            sql.VALUES("id", "#{id,jdbcType=INTEGER}");
        }
        
        if (record.getTitle() != null) {
            sql.VALUES("title", "#{title,jdbcType=VARCHAR}");
        }
        
        if (record.getContent() != null) {
            sql.VALUES("content", "#{content,jdbcType=VARCHAR}");
        }
        
        if (record.getDate() != null) {
            sql.VALUES("date", "#{date,jdbcType=datetime}");
        }
        
        if (record.getUrl() != null) {
            sql.VALUES("url", "#{url,jdbcType=VARCHAR}");
        }
        
        if (record.getImagePath() != null) {
            sql.VALUES("image_path", "#{imagePath,jdbcType=VARCHAR}");
        }
        
        if (record.getType() != null) {
            sql.VALUES("type", "#{type,jdbcType=VARCHAR}");
        }
        
        return sql.toString();
    }

    /**
     * 根据字段是否为空更新 
     * @param record News
     * @return String
     */
    public String updateByPrimaryKeySelective(News record) {
        SQL sql = new SQL();
        sql.UPDATE("news");
        
        if (record.getTitle() != null) {
            sql.SET("title = #{title,jdbcType=VARCHAR}");
        }
        
        if (record.getContent() != null) {
            sql.SET("content = #{content,jdbcType=VARCHAR}");
        }
        
        if (record.getDate() != null) {
            sql.SET("date = #{date,jdbcType=datetime}");
        }
        
        if (record.getUrl() != null) {
            sql.SET("url = #{url,jdbcType=VARCHAR}");
        }
        
        if (record.getImagePath() != null) {
            sql.SET("image_path = #{imagePath,jdbcType=VARCHAR}");
        }
        
        if (record.getType() != null) {
            sql.SET("type = #{type,jdbcType=VARCHAR}");
        }
        
        sql.WHERE("id = #{id,jdbcType=INTEGER}");
        
        return sql.toString();
    }
}